package cc.lj.internal.coupons.domain.martcap.service.impl;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapBatchDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.MartCapDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.convert.CapBatchPOConvertDO;
import cc.lj.internal.coupons.domain.martcap.repository.ICapBatchRepository;
import cc.lj.internal.coupons.domain.martcap.service.ICapBatchDomainService;
import cc.lj.internal.coupons.domain.martcap.service.IMartCapDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.CapBatchPO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 封顶额活动批次表领域服务service实现类
 * @author liangxifeng
 * @date 2021-06-26
 */
@Service
@Slf4j
public class CapBatchDomainServiceImpl implements ICapBatchDomainService {
    //封顶额活动批次表仓储接口
    @Resource(name = "capBatchRepository")
    private ICapBatchRepository repository;


    //封顶额活动领域层
    @Resource(name = "martCapDomainServiceImpl")
    private IMartCapDomainService capDomainService;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @Override
    public PageBean<CapBatchDO> selectPage(PageParam<CapBatchDO> pageParam) {
        PageInfo<CapBatchPO> pageInfo = repository.selectPage(pageConvert(pageParam));

        List<CapBatchDO> list = new ArrayList();
        if(pageInfo.getList().size() > 0)
        {
            for (CapBatchPO capBatchPO : pageInfo.getList()) {
                CapBatchDO activityDO = CapBatchPOConvertDO.INSTANCE.convert(capBatchPO);
                list.add(activityDO);
            }
        }
        //list的po转换为do后再组装PageBean将其返回上一层
        PageBean pageBean = PageBean.createPageBean(pageInfo.getPageNum(),
                pageInfo.getPageSize(), pageInfo.getTotal(), list);
        return pageBean;
    }
    /**
     * 分页DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<CapBatchPO> pageConvert(PageParam<CapBatchDO> pageParam) {
        PageParam<CapBatchPO> pageParamR = new PageParam<>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        CapBatchPO activityPO = CapBatchPOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(activityPO);
        return pageParamR;
    }

    /**
     * 保存活动批次数据
     * @param capBatchDO
     * @return 新增主键
     */
    @Override
    public Integer save(CapBatchDO capBatchDO) {
        /*
        //如果设置了新的卖场分摊，需要验证分摊比之和必须=100
        if( capBatchDO.getIsNewShare() == 1 ) {
            log.info("新增封顶批次DO="+capBatchDO);
            BigDecimal totalShare = capBatchDO.getMerchantShare().add(capBatchDO.getMartShare());
            if(totalShare.floatValue() != 1) {
                log.info("创建封顶额活动批次时，封顶额活动券批次商户分摊+卖场分摊比例总和!=1");
                throw new ServiceException(CoupousExceptionEnum.TOTAL_SHARE_FAILED);
            }
            capBatchDO.setMerchantPriceNew(); //设置商户分摊金额
            capBatchDO.setMartPriceNew(); //设置卖场分金额，注意：这里必须先计算商户分摊，因为此处依赖商户分摊
        }

        //DO to PO
        CapBatchPO capBatchPO = CapBatchPOConvertDO.INSTANCE.convert(capBatchDO);

        //通过券批次主键查询封顶额批次表，如果存在则证明该批次曾经被关联过， 不允许重复关联活动
        CapBatchPO getBatchPO = new CapBatchPO();
        getBatchPO.setBatchId(capBatchPO.getBatchId());
        List<CapBatchPO> capBatchPOList = repository.getList(getBatchPO);
        if( capBatchPOList.size() > 0 ) {
            log.info("创建封顶额活动批次时，发现该批次已经被其他封顶额活动关联,关联信息="+capBatchPOList.get(0));
            throw new ServiceException(CoupousExceptionEnum.TICKET_BATCH_USED);
        }

        //新增批次到数据库
        int lastId = repository.insert(capBatchPO);
        //PO to DO
        CapBatchDO activityBatch = CapBatchPOConvertDO.INSTANCE.convert(capBatchPO);
         */
        return 1;
    }

    /**
     * 通过主键查询一条记录
     * @param id
     * @return
     */
    public CapBatchDO getById(Integer id)
    {
        CapBatchPO capBatchPO = repository.getById(id);
        return CapBatchPOConvertDO.INSTANCE.convert(capBatchPO);
    }

    /**
     * 根据id修改
     * @param capBatchDO
     * @return
     */
    public Integer updateById(CapBatchDO capBatchDO) {
        //如果设置了新的卖场分摊，需要验证分摊比之和必须=100
        /*
        if( capBatchDO.getIsNewShare() == 1 ) {
            log.info("修改封顶批次DO="+capBatchDO);
            BigDecimal totalShare = capBatchDO.getMerchantShare().add(capBatchDO.getMartShare());
            if(totalShare.floatValue() != 1) {
                log.info("修改封顶额活动批次时，封顶额活动券批次商户分摊+卖场分摊比例总和!=1");
                throw new ServiceException(CoupousExceptionEnum.TOTAL_SHARE_FAILED);
            }
            capBatchDO.setMerchantPriceNew(); //设置商户分摊金额
            capBatchDO.setMartPriceNew(); //设置卖场分金额，注意：这里必须先计算商户分摊，因为此处依赖商户分摊
        }else {
            //如果保持原分摊不变，将商户和卖场分摊全部设置为0
            capBatchDO.setMartShare(new BigDecimal(0));
            capBatchDO.setMerchantShare(new BigDecimal(0));
            capBatchDO.setMerchantPrice(new BigDecimal(0));
            capBatchDO.setMartPrice(new BigDecimal(0));
        }
        //通过主键查询封顶额活动,活动已开始不可修改
        MartCapDO capDO = capDomainService.getById(capBatchDO.getCapId());
        if(capDO.getIsOver() == 1) { //活动已结束，不可修改
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_ENDED_NOTUPDATE_BATCH);
        }
        if( capDO.getIsStart() == 1) { //活动已开始，不可修改
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_STARTED);
        }

        CapBatchPO CapBatchPO = CapBatchPOConvertDO.INSTANCE.convert(capBatchDO);
        return repository.updateById(CapBatchPO);*/
        return 0;
    }
    /**
     * 标准批量获取数据的方法
     * @param CapBatchDO
     * @return
     */
    public List<CapBatchDO> getList(CapBatchDO CapBatchDO)
    {
        CapBatchPO CapBatchPO = CapBatchPOConvertDO.INSTANCE.convert(CapBatchDO);
        List<CapBatchPO> list = repository.getList(CapBatchPO);
        List<CapBatchDO> newList = new ArrayList<>();
        for(CapBatchPO item : list)
        {
            newList.add(CapBatchPOConvertDO.INSTANCE.convert(item));
        }
        return newList;
    }

    /**
     * 通过活动主键删除
     * @param id 活动批次表主键
     * @return 删除后的影响行数
     */
    public Integer deleteById(Integer id) {
        //通过主键查询封顶额批次表
        CapBatchPO capBatchPO = repository.getById(id);
        //1.先判断活动是否开始,活动已开始,已结束不可删除
        MartCapDO capDO = capDomainService.getById(capBatchPO.getCapId());
        if(capDO.getIsOver() == 1) { //活动已结束，不可删除
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_ENDED_NOTDELETE_BATCH);
        }
        if( capDO.getIsStart() == 1) { //活动已开始，不可删除
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_STARTED_NOT_DELETE_BATCH);
        }
        //2.逻辑删除活动批次
        Integer rows = repository.deleteById(id);
        return rows;
    }

    @Override
    public String createActivityCode(Integer capBatchId) {
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyyMM");
        String activityNum = "FD" +
                myFmt.format(new Date()) +
                String.format("%07d", capBatchId);
        return activityNum;
    }
}
